﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JuCloudy.Data.Linq
{
    public class SysContainer : JuCloudy.Data.Linq.ISysContainer
    {
        public SysContextDataContext context
        {
            get { return _context; }
        }

        private SysContextDataContext _context = new SysContextDataContext(System.Configuration.ConfigurationManager.AppSettings["SysConnStr"]);

        public List<ViewSysStructureAndCategoryAndAppInfo> GetViewEntities()
        {
            return context.ViewSysStructureAndCategoryAndAppInfo.ToList();
        }

        public ViewSysStructureAndCategoryAndAppInfo GetViewEntity(int StructureId)
        {
            return context.ViewSysStructureAndCategoryAndAppInfo.First(p => p.StructureId == StructureId);
        }

        public List<SysStructure> InsertStructure(SysStructure entity)
        {
            try
            {
                var query = (from c in context.SysStructure
                             where c.StructureId == entity.StructureId
                             select c).ToList();
                if (query.Count < 1)
                {
                    context.SysStructure.InsertOnSubmit(entity);
                }
                else
                {
                    query[0].IsVisable = entity.IsVisable;
                    query[0].StructureHeader = entity.StructureHeader;
                    query[0].StructureOrder = entity.StructureOrder;
                }
                context.SubmitChanges();
                return context.SysStructure.ToList();
            }
            catch
            {
                context.SysStructure.InsertOnSubmit(entity);
                context.SubmitChanges();
                return context.SysStructure.ToList();
            }
        }

        public List<SysStructure> DeleteStructure(SysStructure entity)
        {
            try
            {
                var queruy = context.SysStructure.First(p => p.StructureId == entity.StructureId);
                if (queruy != null)
                {
                    context.SysStructure.DeleteOnSubmit(entity);
                    context.SubmitChanges();
                    return context.SysStructure.ToList();
                }
                else
                {
                    return context.SysStructure.ToList();
                }
            }
            catch
            {
                return context.SysStructure.ToList();
            }
        }

        public List<SysStructure> GetStructureEntities()
        {
            return context.SysStructure.ToList();
        }

        public SysStructure GetStructureEntity(int StructureId)
        {
            var query = context.SysStructure.First(p => p.StructureId == StructureId);
            return query;
        }

        public List<SysCategory> InsertCategory(SysCategory entity)
        {
            try
            {
                var query = context.SysCategory.First(p => p.CategoryId == entity.CategoryId);
                if (query != null)
                {
                    query.CategoryIntruduction = entity.CategoryIntruduction;
                    query.CategoryName = entity.CategoryName;
                    query.CategoryOrder = entity.CategoryOrder;
                    query.IsVisable = entity.IsVisable;
                    context.SubmitChanges();
                    return context.SysCategory.ToList();
                }
                else
                {
                    context.SysCategory.InsertOnSubmit(entity);
                    context.SubmitChanges();
                    return context.SysCategory.ToList();
                }
            }
            catch
            {
                context.SysCategory.InsertOnSubmit(entity);
                context.SubmitChanges();
                return context.SysCategory.ToList();
            }
        }

        public List<SysCategory> DeleteCategory(SysCategory entity)
        {
            try
            {
                var query = context.SysCategory.First(p => p.CategoryId == entity.CategoryId);
                if (query != null)
                {
                    context.SysCategory.DeleteOnSubmit(entity);
                    context.SubmitChanges();
                    return context.SysCategory.ToList();
                }
                else
                {
                    return context.SysCategory.ToList();
                }
            }
            catch
            {
                return context.SysCategory.ToList();
            }
        }

        public List<SysCategory> GetCategoryEntities()
        {
            return context.SysCategory.ToList();
        }

        public SysCategory GetCategoryEntity(int CategoryId)
        {
            return context.SysCategory.First(p => p.CategoryId == CategoryId);
        }

        public List<SysAppInfo> InsertAppInfo(SysAppInfo entity)
        {
            try
            {
                var query = context.SysAppInfo.First(p => p.AppId == entity.AppId);
                if (query != null)
                {
                    query.AppAssembly = entity.AppAssembly;
                    query.AppIntroduction = entity.AppIntroduction;
                    query.AppMainPageName = entity.AppMainPageName;
                    query.AppName = entity.AppName;
                    query.IsEnable = entity.IsEnable;
                    query.IsVisable = entity.IsVisable;
                    query.LastUpdataBy = entity.LastUpdataBy;
                    query.LastUpdataDate = DateTime.Now;
                    query.LastUpdataDateTimestamp = timeStamp.ConvertDateTimeInt((DateTime)query.LastUpdataDate);
                    context.SubmitChanges();
                    return context.SysAppInfo.ToList();
                }
                else
                {
                    entity.CreateDate = DateTime.Now;
                    entity.CreateDateTimestamp = timeStamp.ConvertDateTimeInt((DateTime)entity.CreateDate);

                    entity.LastUpdataDate = entity.CreateDate;
                    entity.LastUpdataDateTimestamp = timeStamp.ConvertDateTimeInt((DateTime)entity.LastUpdataDate);
                    context.SysAppInfo.InsertOnSubmit(entity);
                    context.SubmitChanges();
                    return context.SysAppInfo.ToList();
                }
            }
            catch
            {
                entity.CreateDate = DateTime.Now;
                entity.CreateDateTimestamp = timeStamp.ConvertDateTimeInt((DateTime)entity.CreateDate);

                entity.LastUpdataDate = entity.CreateDate;
                entity.LastUpdataDateTimestamp = timeStamp.ConvertDateTimeInt((DateTime)entity.LastUpdataDate);
                context.SysAppInfo.InsertOnSubmit(entity);
                context.SubmitChanges();
                return context.SysAppInfo.ToList();
            }
        }

        public List<SysAppInfo> DeleteAppInfo(SysAppInfo entity)
        {
            try
            {
                var query = context.SysAppInfo.First(p => p.AppId == entity.AppId);
                if (query != null)
                {
                    context.SysAppInfo.DeleteOnSubmit(entity);
                    context.SubmitChanges();
                    return context.SysAppInfo.ToList();
                }
                else
                {
                    return context.SysAppInfo.ToList();
                }
            }
            catch
            {
                return context.SysAppInfo.ToList();
            }
        }

        public List<SysAppInfo> GetAppInfoEntities()
        {
            return context.SysAppInfo.ToList();
        }

        public SysAppInfo GetAppInfoEntity(int AppInfoId)
        {
            return context.SysAppInfo.First(p => p.AppId == AppInfoId);
        }

        public List<SysAppModule> GetAppModuleEntities(bool? IsEnable)
        {
            if (IsEnable == null)
                return context.SysAppModule.ToList();
            else
                return context.SysAppModule.TakeWhile(p => p.IsEnable == IsEnable).ToList();
        }

        public SysAppModule GetAppModuleEntity(int AppModuleId)
        {
            return context.SysAppModule.First(p => p.Id == AppModuleId);
        }

        public SysAppModule GetNewestAppModuleEntity(int AppId)
        {
            var query = from c in context.SysAppModule
                        where c.AppId == AppId && c.IsEnable==true 
                        select c;
            try
            {
                query = query.OrderByDescending(p => p.CreateDateTimestamp);
            }
            catch { }
            return query.ToList()[0];
        }

        public int InsertAppModule(SysAppModule entity)
        {
            try
            {
                var query = context.SysAppModule.First(p => p.AppId == entity.AppId);
                if (query != null)
                    entity.IsUpdata = true;
                else
                    entity.IsUpdata = false;

                context.SysAppModule.InsertOnSubmit(entity);
                context.SubmitChanges();
                return entity.Id;
            }
            catch{
                entity.IsUpdata = false;
                context.SysAppModule.InsertOnSubmit(entity);
                context.SubmitChanges();
                return entity.Id;
            }
        }
    }
}
